package cn.schoolwow.util.test.module.query.jsonarray.flow.group;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Arrays;
import java.util.List;

public class InitialUserRoleNameJSONArrayDataFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        JSONArray userArray = (JSONArray) flowContext.checkData("userArray");

        List<String> roleNameList = Arrays.asList("超级管理员", "管理员");
        int mid = userArray.size()/2;
        for(int i=0;i<mid;i++){
            JSONObject user = userArray.getJSONObject(i);
            user.put("roleName", roleNameList.get(0));
        }
        for(int i=mid;i<userArray.size();i++){
            JSONObject user = userArray.getJSONObject(i);
            user.put("roleName", roleNameList.get(1));
        }
        flowContext.putTemporaryData("roleNameList", roleNameList);
    }

    @Override
    public String name() {
        return "初始化用户角色列表数据";
    }
}
